package com.example.log.alarm.repository;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.example.log.alarm.advice.exeception.ThrowableInfo;
import com.example.log.alarm.advice.logger.LogInfo;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328
 * @create: 2023-05-05 16:46
 **/
public interface Repository {

    void putThrowable(Map<String, Object> map);

    default void putThrowable(List<Map<String, Object>> throwableList) {
        throwableList.forEach(this::putThrowable);
    }

    default Set<String> getThrowableClasses() {
        return Collections.EMPTY_SET;
    }

    default Collection<ThrowableInfo> getThrowableInfoList() {
        return getThrowableInfoList(null);
    }

    default Collection<ThrowableInfo> getThrowableInfoList(String clazzName) {
        return Collections.EMPTY_LIST;
    }

    default String getStackTrace(String stackTraceId) {
        return this.getStackTraces().get(stackTraceId);
    }

    default Map<String, String> getStackTraces() {
        return Collections.EMPTY_MAP;
    }

    default void clear() {}

    void putLogger(ILoggingEvent event);

    default Collection<LogInfo> getLogInfos() {
        return Collections.EMPTY_LIST;
    }

    default Collection<LogInfo> getWarningLogInfos() {
        Collection<LogInfo> logInfos = this.getLogInfos();
        if (CollectionUtils.isEmpty(logInfos)) {
            return Collections.EMPTY_LIST;
        }
        return logInfos.stream().filter(e-> Level.WARN.levelStr.equals(e.getLevel())).collect(Collectors.toList());
    }

    default Collection<LogInfo> getErrorLogInfos() {
        Collection<LogInfo> logInfos = this.getLogInfos();
        if (CollectionUtils.isEmpty(logInfos)) {
            return Collections.EMPTY_LIST;
        }
        return logInfos.stream().filter(e-> Level.ERROR.levelStr.equals(e.getLevel())).collect(Collectors.toList());
    }

}
